package com.itwlj.controller.user;

import com.itwlj.context.BaseContext;
import com.itwlj.dto.CommentDTO;
import com.itwlj.dto.OrderPageQueryDTO;
import com.itwlj.entity.User;
import com.itwlj.result.PageResult;
import com.itwlj.result.Result;
import com.itwlj.service.OrderService;
import com.itwlj.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会员中心
 */
@RestController
@RequestMapping("/member")
@Api(tags = "C端会员中心相关接口")
@Slf4j
public class MemberController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    /**
     * 查询会员信息
     *
     * @return
     */
    @GetMapping
    public Result<User> MemberInfo() {
        User user = userService.getById(BaseContext.getCurrentId());
        log.info("查询会员信息：{}", BaseContext.getCurrentId());
        return Result.success(user);
    }

    /**
     * 查询用户订单
     *
     * @param orderPageQueryDTO
     * @return
     */
    @GetMapping("/order")
    public Result<PageResult> getUserOrders(OrderPageQueryDTO orderPageQueryDTO) {
        log.info("查询用户订单：{}", orderPageQueryDTO);
        orderPageQueryDTO.setUserId(BaseContext.getCurrentId());
        return Result.success(orderService.pageQuery(orderPageQueryDTO));
    }

    /**
     * 用户取消订单
     *
     * @param id
     * @return
     */
    @PutMapping("/order/cancel")
    @ApiOperation("用户取消订单")
    public Result userCancelOrder(Long id) {
        log.info("用户取消订单：{}", id);
        orderService.userCancelOrder(id);
        return Result.success();
    }

    /**
     * 确认收货
     *
     * @param id
     * @return
     */
    @PutMapping("/order/confirm")
    @ApiOperation("确认收货")
    public Result confirmReceive(Long id) {
        log.info("确认收货：{}", id);
        orderService.confirmReceive(id);
        return Result.success();
    }

    /**
     * 评论商品
     *
     * @param commentDTO
     * @return
     */
    @PostMapping("/order/comment")
    @ApiOperation("评论商品")
    public Result commentOrder(@RequestBody CommentDTO commentDTO) {
        log.info("评论商品,sku id：{}", commentDTO.getSkuId());
        orderService.commentOrder(commentDTO);
        return Result.success();
    }
}
